{% extends "base.html" %}

{% block title %}{{ database }}{% endblock %}

{% block extra_head %}
{{- super() -}}
{% include "_codemirror.html" %}
{% endblock %}

{% block body_class %}db db-{{ database|to_css_class }}{% endblock %}

{% block crumbs %}
{{ crumbs.nav(request=request, database=database) }}
{% endblock %}

{% block content %}
<div class="page-header" style="border-color: #{{ database_color }}">
    <h1>{{ metadata.title or database }}{% if private %} 🔒{% endif %}</h1>
</div>
{% set action_links, action_title = database_actions(), "Database actions" %}
{% include "_action_menu.html" %}

{{ top_database() }}

{% block description_source_license %}{% include "_description_source_license.html" %}{% endblock %}

{% if allow_execute_sql %}
    <form class="sql core" action="{{ urls.database(database) }}/-/query" method="get">
        <h3>Custom SQL query</h3>
        <p><textarea id="sql-editor" name="sql">{% if tables %}select * from {{ tables[0].name|escape_sqlite }}{% else %}select sqlite_version(){% endif %}</textarea></p>
        <p>
            <button id="sql-format" type="button" hidden>Format SQL</button>
            <input type="submit" value="Run SQL">
        </p>
    </form>
{% endif %}

{% if attached_databases %}
    <div class="message-info">
        <p>The following databases are attached to this connection, and can be used for cross-database joins:</p>
        <ul class="bullets">
            {% for db_name in attached_databases %}
                <li><strong>{{ db_name }}</strong> - <a href="{{ urls.database(db_name) }}/-/query?sql=select+*+from+[{{ db_name }}].sqlite_master+where+type='table'">tables</a></li>
            {% endfor %}
        </ul>
    </div>
{% endif %}

{% if queries %}
    <h2 id="queries">Queries</h2>
    <ul class="bullets">
        {% for query in queries %}
            <li><a href="{{ urls.query(database, query.name) }}{% if query.fragment %}#{{ query.fragment }}{% endif %}" title="{{ query.description or query.sql }}">{{ query.title or query.name }}</a>{% if query.private %} 🔒{% endif %}</li>
        {% endfor %}
    </ul>
{% endif %}

{% if tables %}
<h2 id="tables">Tables <a style="font-weight: normal; font-size: 0.75em; padding-left: 0.5em;" href="{{ urls.database(database) }}/-/schema">schema</a></h2>
{% endif %}

{% for table in tables %}
{% if show_hidden or not table.hidden %}
<div class="db-table">
    <h3><a href="{{ urls.table(database, table.name) }}">{{ table.name }}</a>{% if table.private %} 🔒{% endif %}{% if table.hidden %}<em> (hidden)</em>{% endif %}</h3>
    <p><em>{% for column in table.columns %}{{ column }}{% if not loop.last %}, {% endif %}{% endfor %}</em></p>
    <p>{% if table.count is none %}Many rows{% elif table.count == count_limit + 1 %}&gt;{{ "{:,}".format(count_limit) }} rows{% else %}{{ "{:,}".format(table.count) }} row{% if table.count == 1 %}{% else %}s{% endif %}{% endif %}</p>
</div>
{% endif %}
{% endfor %}

{% if hidden_count and not show_hidden %}
    <p>... and <a href="{{ urls.database(database) }}?_show_hidden=1">{{ "{:,}".format(hidden_count) }} hidden table{% if hidden_count == 1 %}{% else %}s{% endif %}</a></p>
{% endif %}

{% if views %}
    <h2 id="views">Views</h2>
    <ul class="bullets">
        {% for view in views %}
            <li><a href="{{ urls.database(database) }}/{{ view.name|urlencode }}">{{ view.name }}</a>{% if view.private %} 🔒{% endif %}</li>
        {% endfor %}
    </ul>
{% endif %}

{% if allow_download %}
    <p class="download-sqlite">Download SQLite DB: <a href="{{ urls.database(database) }}.db" rel="nofollow">{{ database }}.db</a> <em>{{ format_bytes(size) }}</em></p>
{% endif %}

{% include "_codemirror_foot.html" %}

{% endblock %}
